﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DotNetSpeech;

namespace FingerprintAttendance
{
    public partial class FingerprintForm : Form
    {
        public AxZKFPEngXControl.AxZKFPEngX axZKFPEngX;
        public Image ImgFingerpring;
        public string FingerId;
        public string UserType;
        public string UserId;

        SpeechVoiceSpeakFlags SVSF = SpeechVoiceSpeakFlags.SVSFlagsAsync;
        SpVoice Speech = new SpVoice();

        object fpTemplate;//指纹模板
        byte[] fpImg = null;//指纹图像

        public FingerprintForm()
        {
            InitializeComponent();

        }

        private void FingerprintForm_Load(object sender, EventArgs e)
        {
            InitSensor();
        }

        public void InitSensor()
        {
            this.axZKFPEngX.OnFeatureInfo += new AxZKFPEngXControl.IZKFPEngXEvents_OnFeatureInfoEventHandler(axZKFPEngX_OnFeatureInfo);
            this.axZKFPEngX.OnImageReceived += new AxZKFPEngXControl.IZKFPEngXEvents_OnImageReceivedEventHandler(axZKFPEngX_OnImageReceived);
            this.axZKFPEngX.OnEnroll += new AxZKFPEngXControl.IZKFPEngXEvents_OnEnrollEventHandler(axZKFPEngX_OnEnroll);
            this.axZKFPEngX.OnEnrollToFile += new AxZKFPEngXControl.IZKFPEngXEvents_OnEnrollToFileEventHandler(axZKFPEngX_OnEnrollToFile);

            tsslMsg.Text = "设备已经连接";
            label4.Text = "设备数量:" + axZKFPEngX.SensorCount.ToString(); //设备数量
            label5.Text = "指纹取样号:" + axZKFPEngX.SensorIndex.ToString(); //登记指纹取样序号
            tsslDeviceNum.Text = "设备序号:" + axZKFPEngX.SensorSN; //设备序号
            label7.Text = "图片宽:" + axZKFPEngX.ImageWidth.ToString(); //图片宽 
            label8.Text = "图片高:" + axZKFPEngX.ImageHeight.ToString(); //图片高
           
            axZKFPEngX.RegTplFileName = "c:\\T.template";

            //是否登记指纹
            if (axZKFPEngX.IsRegister) axZKFPEngX.CancelEnroll();//取消当前指纹登记状态
            axZKFPEngX.BeginEnroll();

            Speech.Speak("开始登记指纹,请按" + txtFingerName.Text + axZKFPEngX.EnrollCount.ToString() + "次", SVSF);
        }

        private void DisposeSensor()
        {
            this.axZKFPEngX.OnFeatureInfo -= new AxZKFPEngXControl.IZKFPEngXEvents_OnFeatureInfoEventHandler(axZKFPEngX_OnFeatureInfo);
            this.axZKFPEngX.OnImageReceived -= new AxZKFPEngXControl.IZKFPEngXEvents_OnImageReceivedEventHandler(axZKFPEngX_OnImageReceived);
            this.axZKFPEngX.OnEnroll -= new AxZKFPEngXControl.IZKFPEngXEvents_OnEnrollEventHandler(axZKFPEngX_OnEnroll);
            this.axZKFPEngX.OnEnrollToFile -= new AxZKFPEngXControl.IZKFPEngXEvents_OnEnrollToFileEventHandler(axZKFPEngX_OnEnrollToFile);
           
        }

        private void FingerprintForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            DisposeSensor();

        }

        public void axZKFPEngX_OnImageReceived(object sender, AxZKFPEngXControl.IZKFPEngXEvents_OnImageReceivedEvent e)
        {

            //接收指纹信息,并显示
 
            string mvPath = "fingerprint.bmp";
            if (pbFingerprint.Image != null)
            {
                pbFingerprint.Image.Dispose();//释放文件资源
                pbFingerprint.Image = null;//清除图片
            }
           
            axZKFPEngX.SaveBitmap(mvPath);//保存新文件

            Image Img = System.Drawing.Bitmap.FromFile(mvPath);
            MemoryStream Ms = new MemoryStream();
            //将图像保存至内存流
            Img.Save(Ms, System.Drawing.Imaging.ImageFormat.Bmp);
            Img.Dispose();

            FileStream stream = new FileStream(mvPath, FileMode.Open, FileAccess.Read);
            fpImg = new byte[stream.Length];
            stream.Read(fpImg, 0, (int)Ms.Length);
            stream.Close();

            pbFingerprint.Image = Image.FromStream(Ms);//显示图片
            pbFingerprint.Refresh();
            Ms.Close();
            System.IO.File.Delete(mvPath);//删除图片文件
        }

        public void axZKFPEngX_OnFeatureInfo(object sender, AxZKFPEngXControl.IZKFPEngXEvents_OnFeatureInfoEvent e)
        {
           
            int j = 0;
            String sTemp =  "本次指纹采集质量";
            //aQuality(0-好的指纹,1-特征点不够,2-其它因导致不能取得指纹特征)
            if (e.aQuality != 0)
            {
                sTemp = sTemp + "不合格!";
                Speech.Speak("本次采集指纹不合格,请从按", SVSF);
            }
            else
            {
                sTemp = sTemp + "合格!";
                if (axZKFPEngX.IsRegister)
                {
                    j = axZKFPEngX.EnrollIndex - 1;
                    sTemp = sTemp + "还需采集 " + j.ToString() + " 次!";
                    if (j != 0)
                        Speech.Speak("再按一次", SVSF);
                }
            }

            tsslMsg.Text = sTemp;

           

        }


        //登记指纹结束时调用 OnFeatureInfo三次后调用
        public void axZKFPEngX_OnEnroll(object sender, AxZKFPEngXControl.IZKFPEngXEvents_OnEnrollEvent e)
        {
            if (e.actionResult)
            {
                
                fpTemplate = e.aTemplate;
               
                string fpEncode = axZKFPEngX.EncodeTemplate1(fpTemplate);//将控件使用的Variant模板ASour转换为BASE64格式的模板字符串

                //添加到数据库
                try
                {
                    int result = DataOperation.AddFingerprint(UserId, FingerId,UserType,txtUserName.Text,txtUserNumber.Text, fpEncode,fpImg); 
                    if (result == 0)
                    {
                        Speech.Speak("指纹采集失败,请重新采集", SVSF);
                        //MessageBox.Show("指纹信息添加到数据失败！", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Restart();
                        return;
                    }

                    Speech.Speak("指纹采集完成", SVSF);
                    //MessageBox.Show("指纹采集完成", "提示", MessageBoxButtons.OK);
                    ImgFingerpring = pbFingerprint.Image;
                    DisposeSensor();
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (DBHelper.conn != null && DBHelper.conn.State == ConnectionState.Open)
                        DBHelper.conn.Close();
                }

              
               
            }
        }


        public void axZKFPEngX_OnEnrollToFile(object sender, AxZKFPEngXControl.IZKFPEngXEvents_OnEnrollToFileEvent e)
        {
            //if (e.actionResult)
            //{

            //    MessageBox.Show("Register Success", "Warn", MessageBoxButtons.OK);
            //}
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 重取指纹方法
        /// </summary>
        private void Restart()
        {
            if (pbFingerprint.Image != null)
            {
                pbFingerprint.Image.Dispose();
                pbFingerprint.Image = null;
            }
           
            tsslMsg.Text = "请按指纹！";
            axZKFPEngX.BeginEnroll();//开始登记指纹，登记结束后发生OnEnroll事件。
            Speech.Speak("重新登记指纹,请按" + txtFingerName, SVSF);
        }

        private void btnReIni_Click(object sender, EventArgs e)
        {
            Restart();
        }
    }
}
